<?php

namespace app\libs\PaypalSDK\builder;

use app\model\LineItems;
use app\model\Orders;
use think\helper\Arr;
use think\Request;

class PaypalBuilder{
    protected Orders $order;
    protected  Request $request;
    public function __construct( Orders $order, Request $request)
    {
        $this->order = $order;
        $this->request = $request;
    }
    public function getItemLines()
    {
            $lines = [];
            $subtotal_price = '0.00';
            $this->order->items()->select()->each(function (LineItems $item) use (&$lines,&$subtotal_price){
                $lines[] = [
                    'name' => mb_substr($item->title, 0, 120),
                    'description' =>$item->variant_title,
                    'quantity' => $item->quantity,
                    'unit_amount' => ['value' => $item->price, 'currency_code' => $this->order->present_currency],
                    'sku'=>$item->sku,
                ];
                $sub_price = bcmul($item->price,(string)$item->quantity,2);
                $subtotal_price = bcadd($sub_price,$subtotal_price,2);
            });
            return [$lines,$subtotal_price];
    }

    public function getTransactions()
    {
        list($lines, $subtotal_price) = $this->getItemLines();
        $totalPrice = bcadd($subtotal_price,$this->order->shipping_price_present,2);
        list($total_money,$tax_price) = Orders::calculateTotalPrice($totalPrice);
        $breakDown = [
            'item_total'=>['value'=>$subtotal_price, 'currency_code'=>$this->order->present_currency,],
        ];
        if($this->order->shipping_price_present > 0){
            $breakDown['shipping'] = ['value'=>$this->order->shipping_price_present,'currency_code'=>$this->order->present_currency];
        }
        if($tax_price > 0){
            $breakDown['tax_total'] = ['value'=>$tax_price,'currency_code'=>$this->order->present_currency];
        }
        $transactions = [
            [
                'amount'=>[
                    'value'=>$total_money,
                    'currency_code'=>$this->order->present_currency,
                    'breakdown'=>$breakDown,
                ],
                'items'=>$lines,

            ]
        ];
        if($this->order->shippingAddress) $transactions[0]['shipping'] = $this->getShippingAddress();
        return $transactions;
    }

    public function getShippingAddress()
    {

        return [
            'type'=>'SHIPPING',
            'name'=>[
                'full_name'=>$this->order->shippingAddress->name,
            ],
            'address'=>[
                'country_code'=>$this->order->shippingAddress->country_code,
                'postal_code'=>$this->order->shippingAddress->zip,
                'address_line_1'=>$this->order->shippingAddress->address1,
                'admin_area_1'=>$this->order->shippingAddress->province,
                'admin_area_2'=>$this->order->shippingAddress->city,
            ],
        ];
    }



    public function getRequest()
    {
        $query = $this->request->all();
        if($query){
            $query = Arr::only($query,['loading_path','return_path']);
        }
        return $query;
    }

    public function get_application_context(bool $isShipping=false):array
    {
        list($return_url,$cancel_url) = Orders::getContextApplicationUrl($this->order,$this->request);
        $res = [
            'return_url'=>$return_url,
            'cancel_url'=>$cancel_url,
        ];
        if($isShipping) $res['shipping_preference'] = 'SET_PROVIDED_ADDRESS';
        return $res;
    }



}